package com.testingedu.jdbcDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.testingedu.common.QueryResult;
import com.testingedu.model.TMenu;
import com.testingedu.model.TStudyState;
import com.testingedu.model.TUser;
import com.testingedu.utils.Format;

/**
 * 表TStudyState jdbc类 最后修改：2012-11-23 修改人:bfc
 * 
 * @author Administrator
 * 
 */
public class TStudyStateDao extends BaseDao<TMenu> {
	public TStudyStateDao() {
		clazz = TStudyState.class;
	}

	/**
	 * 增加学习状态
	 * 
	 * @param tStudyState
	 * @return
	 */
	public int add(TStudyState tStudyState) throws Exception{
		String sql = "insert into t_study_state (state,add_time,remark,user_id) value(?,?,?,?)";
		List<Object> values = new ArrayList<Object>();
		values.add(tStudyState.getState());
		values.add(Format.formatDateDay(new Date()));
		values.add("无");
		values.add(tStudyState.gettUser().getId());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
		return getMaxId();
	}

	private int getMaxId() throws Exception{
		String sql = "select max(id) as _id from t_study_state";
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql, null);
			if (rs.next()) {
				return rs.getInt("_id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
		return 0;
	}

	public void delete(int id) throws Exception{
		String sql = "delete from t_study_state where id=?";
		try {
			this.myJdbcSetValuesAndExecuteDelete(sql, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
	}

	public void update(TStudyState tStudyState) throws Exception{
		String sql = "update  t_study_state set state=? where id=?";
		List<Object> values = new ArrayList<Object>();
		values.add(tStudyState.getState());
		values.add(tStudyState.getId());
		try {
			this.myJdbcSetValuesAndExecuteSaveOrUpdateOrDelete(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			this.close();
		}
	}

	public void list(QueryResult queryResult, TStudyState tStudyState, Integer userId) throws Exception{
		StringBuffer sql = new StringBuffer(
				"select * from t_study_state tss where 1=1");
		List<Object> values = new ArrayList<Object>();
		if (tStudyState != null) {
			if(tStudyState.gettUser() != null && tStudyState.gettUser().getName() != null && tStudyState.gettUser().getName().trim().length() > 0) {
				sql.append( " and tss.user_id in (select tu.id from t_user tu where tu.name like ?)");
				values.add("%"+tStudyState.gettUser().getName()+"%");
			}
		}
		if(userId != null && userId > 0) {
			sql.append(" and tss.user_id = ?");
			values.add(userId);
		}
		sql.append(" limit ?,?");
		List<Object> countValues = new ArrayList<Object>(values);
		values.add(queryResult.getStartRow());
		values.add(queryResult.getPageSize());
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.toString(), values);
			queryResult.setResultList(_resultSetToList(rs));
		} finally {
			close();
		}
		sql.replace(7, 8, "count(*)");
		try {
			this.myJdbcSetValuesAndExecuteQuery(sql.substring(0,
					sql.length() - 10), countValues);
			if (rs.next()) {
				queryResult.setAllCount(new Long(rs.getInt(1)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			close();
		}
	}

	public TStudyState info(int id) throws Exception{
		String sql = "select * from t_study_state tp where tp.id = ?";
		List<TStudyState> result = null;
		try {
			this.myJdbcSetValuesAndExecuteLoad(sql, id);
			result = _resultSetToList(rs);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			close();
		}
		if (result != null && result.size() > 0) {
			return result.get(0);
		}
		return null;
	}

	public List<TStudyState> _resultSetToList(ResultSet rs) throws Exception{
		List<TStudyState> result = new ArrayList<TStudyState>();
		TUserDao tUserDao = new TUserDao();
		try {
			while (rs.next()) {
				TStudyState tStudyState = new TStudyState();
				tStudyState.setId(rs.getInt("id"));
				tStudyState.setAddTime(rs.getDate("add_time"));
				tStudyState.setState(rs.getString("state"));
				int userId = rs.getInt("user_id");
				TUser tUser = tUserDao.info(userId);
				tStudyState.settUser(tUser);
				result.add(tStudyState);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception();
		}
		return result;
	}
}
